﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace GDI
{
    public partial class GraphControl : UserControl
    {
        public double Ymax
        {
            get { return f_DataChart.ChartAreas[0].AxisY.Maximum; }
            set { f_DataChart.ChartAreas[0].AxisY.Maximum = value; }
        }

        public double Ymin
        {
            get { return f_DataChart.ChartAreas[0].AxisY.Minimum; }
            set { f_DataChart.ChartAreas[0].AxisY.Minimum = value; }
        }

        public double Xmax
        {
            get { return f_DataChart.ChartAreas[0].AxisX.Maximum; }
            set { f_DataChart.ChartAreas[0].AxisX.Maximum = value; }
        }

        public double Xmin
        {
            get { return f_DataChart.ChartAreas[0].AxisX.Minimum; }
            set { f_DataChart.ChartAreas[0].AxisX.Minimum = value; }
        }

        public string X_Name
        {
            get { return f_DataChart.ChartAreas[0].AxisX.Title; }
            set { f_DataChart.ChartAreas[0].AxisX.Title = value; }
        }

        public string Y_Name
        {
            get { return f_DataChart.ChartAreas[0].AxisY.Title; }
            set { f_DataChart.ChartAreas[0].AxisY.Title = value; }
        }


        public GraphControl() { InitializeComponent(); }

        public void Plot(Func<double, double> f, double x_min, double x_max, int PointsCount = 1000,
            string Name = "", Color color = default(Color), int Width = 1)
        {
            var x0 = x_min;
            var dx = (x_max - x0) / (PointsCount - 1);
            var x = new double[PointsCount];
            var y = new double[PointsCount];

            for(var i = 0; i < PointsCount; i++, x0 += dx)
                y[i] = f(x[i] = x0);

            Plot(x, y, Name, color, Width);
        }

        public void Plot(Func<double, double> f, int PointsCount = 1000,
            string Name = "", Color color = default(Color), int Width = 1)
        {
            var x0 = Xmin;
            var dx = (Xmax - x0) / (PointsCount - 1);
            var x = new double[PointsCount];
            var y = new double[PointsCount];

            for(var i = 0; i < PointsCount; i++, x0 += dx)
                y[i] = f(x[i] = x0);

            Plot(x, y, Name, color, Width);
        }

        public void Plot(double[] X, double[] Y,
            string Name = "", Color color = default (Color), int Width = 1)
        {
            var series = new Series(Name)
            {
                BorderWidth = Width,
                ChartType = SeriesChartType.FastLine,
                LegendText = Name
            };
            if(color != default(Color))
                series.Color = color;

            f_DataChart.Series.Add(series);
            for(var i = 0; i < X.Length; i++)
                if(!double.IsInfinity(Y[i]))
                    series.Points.AddXY(X[i], Y[i]);
            ScaleGraph();
        }

        public void ScaleGraph()
        {
            //var a = f_DataChart.ChartAreas[0];
            //a.RecalculateAxesScale();

            //f_DataChart.Series.Foreach(ss =>
            //{
            //    var minX = ss.Points.Min(p => p.XValue);
            //    var maxX = ss.Points.Max(p => p.XValue);
            //    var minY = ss.Points.Min(p => p.YValues[0]);
            //    var maxY = ss.Points.Max(p => p.YValues[0]);

            //    var area = f_DataChart.ChartAreas[ss.ChartArea];
            //    area.AxisX.Minimum = Math.Min(area.AxisX.Minimum, minX);
            //    area.AxisX.Maximum = Math.Max(area.AxisX.Maximum, maxX);
            //    area.AxisY.Minimum = Math.Min(area.AxisY.Minimum, minY);
            //    area.AxisY.Maximum = Math.Max(area.AxisY.Maximum, maxY);
            //});
        }

        private void ScreenShot_ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if(f_SaveScreenShotFileDialog.ShowDialog(this) != DialogResult.OK) return;
            f_DataChart.SaveImage(f_SaveScreenShotFileDialog.FileName, ChartImageFormat.Png);
        }
    }
}
